package com.codered;

import java.util.Arrays;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.util.Log;

public class RecurringService extends Service {

	/**
	 * Variables
	 */
	
	// Constants
	protected static String TAG = "RecurringService";
	protected static boolean DEBUG = true;

	
	
	
	
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate() {
        if (DEBUG) Log.i(TAG, "-----onCreate fired-----");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	if (DEBUG) Log.i(TAG, "-----onStart fired-----");
    	
        String[] filelist = fileList();
        if (DEBUG) Log.i(TAG, "Filename: " + Arrays.toString(filelist));

        // TODO: Try to establish Bluetooth connectivity with local devices using this app
        
        // TODO:
        
        /*
        Map params = new HashMap();
        Uri uri = Uri.parse("the-uri-to-your-file");
        params.put("AWSAccessKeyId", "our-aws-access-key");
        params.put("Content-Type", "image/jpeg");
        params.put("policy", "some-policy-defined-by-yourself");
        params.put("Filename", "photo.jpg");
        params.put("key", "images/photo.jpg");
        params.put("acl", "private");
        params.put("signature", "some-signature-defined-by-yourself");
        params.put("success_action_status", "201");
        
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
          urlConnection.setDoOutput(true); // going to be a POST
          urlConnection.setChunkedStreamingMode(0); // 0 = system default chunk size

          OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
          writeStream(out);

          InputStream in = new BufferedInputStream(urlConnection.getInputStream());
          if (!url.getHost().equals(urlConnection.getURL().getHost())) {
        	  
          }
          readStream(in);
         finally {
          urlConnection.disconnect();
        }
      }
      */
        
    	// Start a new thread to attempt to transmit files
    	// TODO: Make this happen on a thread
    	/*
    	new Thread(new Runnable() {
            public void run() {
                final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
                mImageView.post(new Runnable() {
                    public void run() {
                        mImageView.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();
        */
    	stopSelf(startId);
    	
    	return START_NOT_STICKY;
    }
    
    @Override
	public IBinder onBind(Intent arg0) {
    	if (DEBUG) Log.i(TAG, "-----onBind fired-----");
		// TODO Auto-generated method stub
		return null;
	}
    
    @Override
    public void onDestroy() {
    	if (DEBUG) Log.i(TAG, "-----onDestroy fired-----");
    }
    

    
    
    
    
    
    
    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
    }
}
